package org.moflon.boardgamegui.controller;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EPackage;

import GameModel.Board;
import GameModel.Field;
import GameModel.Figure;
import GameModel.GameModelFactory;
import GameModel.GameModelPackage;
import GameModel.Helper.GameModelUtils;
import GameModel.Helper.HelperFactory;

public class Main extends GenericController<Board, Field, Figure> {

	public Main(int width, int height) {
		super(width, height);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Main m = new Main(3, 3);
	}

	@Override
	public EPackage getEPackage() {
		return GameModelPackage.eINSTANCE;
	}

	@Override
	public int getBoardWidth() {
		return getBoard().getWidth();
	}

	@Override
	public int getBoardHeight() {
		return getBoard().getHeight();
	}

	@Override
	public EList<Field> getFields() {
		return getBoard().getFields();
	}

	@Override
	public Figure getFigure(Field field) {
		return field.getFigure();
	}

	@Override
	public int getFieldXCoordinate(Field field) {
		return field.getX();
	}

	@Override
	public int getFieldYCoordinate(Field field) {
		return field.getY();
	}

	@Override
	public EFactory getEFactory() {
		return GameModelFactory.eINSTANCE;
	}

	@Override
	public void addFigureToField(Figure figure, Field field) {
		field.setFigure(figure);
	}

	@Override
	public List<EClass> getConcreteFigureClasses() {
		List<EClass> result = new LinkedList<EClass>();
		EList<EClassifier> classifier = GameModelPackage.eINSTANCE.getEClassifiers();
		
		for(EClassifier cl : classifier) {
			if(GameModelPackage.eINSTANCE.getFigure().isSuperTypeOf((EClass) cl) && !((EClass) cl).isAbstract()) {
				result.add((EClass) cl);
			}
		}

		return result;
	}

	@Override
	public Board makeNewBoard(int width, int height) {
		Board newBoard = GameModelFactory.eINSTANCE.createBoard();
		newBoard.setHeight(height);
		newBoard.setWidth(width);
		
		newBoard.initialize(HelperFactory.eINSTANCE.createGameModelUtils(), height, width);
		return newBoard;
	}

	@Override
	public void fieldPressed(Field field) {
		getBoard().fieldSelected(field);
		updateView();
	}

}
